

//
//  CABasicAnimationController.m
//  界面动画集
//
//  Created by YYY on 2017/7/3.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

#import "CABasicAnimationController.h"

@interface CABasicAnimationController ()
{
    NSArray *transformArray;
}
@property(nonatomic,strong)CALayer *layer;

@property(nonatomic,strong)UIButton *rightBtn;

@end

@implementation CABasicAnimationController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    transformArray = @[@"position位移",@"rotation旋转",@"shake抖动"];
    self.title = @"CABasicAnimation";
    [self.view.layer addSublayer:self.layer];
    for (int i=0; i<transformArray.count; i++)
    {
        UIButton *transform = [UIButton buttonWithType:UIButtonTypeCustom];
        transform.frame = CGRectMake(10+i*100, 70, 90, 30);
        [transform setTitle:transformArray[i] forState:UIControlStateNormal];
        transform.titleLabel.font = [UIFont systemFontOfSize:14];
        [transform setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [transform addTarget:self action:@selector(transformAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:transform];
    }
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.rightBtn];
    
    // Do any additional setup after loading the view.
}

-(UIButton *)rightBtn
{
    if (!_rightBtn)
    {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = CGRectMake(0, 0, 50, 30);
        _rightBtn.backgroundColor = [UIColor redColor];
        
        [_rightBtn setTitle:@"测试" forState:UIControlStateNormal];
    }
    return _rightBtn;
}

-(void)transformAction:(UIButton *)transformBtn
{
    if([transformBtn.currentTitle isEqualToString:@"position位移"])
    {
        //位移动画
        CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
        //是否移除变化后的状态
        positionAnimation.removedOnCompletion = NO;
        //动画前后停留的状态
        positionAnimation.fillMode = kCAFillModeForwards;
       
        positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        //持续时长
        positionAnimation.duration = 3;
        positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 150)];
        [self.layer addAnimation:positionAnimation forKey:nil];
    }
    else if([transformBtn.currentTitle isEqualToString:@"rotation旋转"])
    {
        //旋转动画
        CABasicAnimation *rotaionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotaionAnimation.removedOnCompletion = NO;
        rotaionAnimation.fillMode = kCAFillModeForwards;
        rotaionAnimation.repeatCount = HUGE;
        //rotaionAnimation.duration = 2;
        rotaionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        rotaionAnimation.toValue = @(2*M_PI);
        [self.layer addAnimation:rotaionAnimation forKey:nil];
    }
    else if([transformBtn.currentTitle isEqualToString:@"shake抖动"])
    {
        //抖动动画
        CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//        shakeAnimation.fillMode = kCAFillModeForwards;
//        shakeAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
         //重复次数
        shakeAnimation.repeatCount = 2;
         //动画持续时间
        shakeAnimation.duration = 0.3;
         //抖动角度 1/180
        shakeAnimation.fromValue = @(-M_1_PI/2);
        shakeAnimation.toValue = @(M_1_PI/2);
        //结束后执行逆动画
        shakeAnimation.autoreverses = YES;
        self.layer.anchorPoint = CGPointMake(0.5, 0.5);
        [self.layer addAnimation:shakeAnimation forKey:nil];
    }
    
}

-(CALayer *)layer
{
    if (!_layer)
    {
        _layer = [[CALayer alloc]init];
        //_layer.position = self.view.layer.position;
        _layer.frame = CGRectMake(30,230, 100, 100);
        //_layer.backgroundColor = [UIColor purpleColor].CGColor;
        _layer.cornerRadius = 10;
        _layer.shadowOffset = CGSizeMake(4, 4);
        _layer.shadowColor = [UIColor redColor].CGColor;
        _layer.shadowOpacity = 0.7;
        _layer.shadowRadius = 12;
        _layer.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pear"]].CGColor;
        
    }
    return _layer;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
